package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.DeptmentListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.ZTreeNode;
import java.util.HashMap;

/**
 * 部门单选列表
 * 开发者: HuXiaoDan
 * 日期: 2017/1/4 10:26
 */
public class DeptmentListFragment extends BaseListFragment {

  private CurrentUser mUser;
  private DeptmentListAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    mUser = CurrentUser.newInstance(getActivity());
    //部门列表角色类型参数置为空,原因: 传角色数据返回和网页不一致
    BaseVolleyActivity.setRoleTypeFlag("");
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {
    return ApiConst.URL_TODO_DEPT;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();

    params.put("memCode", mUser.getMemberCode());
    params.put("type", "mobile");
    params.put("ifContainStaff", "false");
    params.put("orderField", "IFNULL(PORG_CODE,'0'),ORDER_NO");
    params.put("orderSeq", "asc");
    params.put("noManageFlag", "T");
    return params;
  }

  @Override public void resolveResponse(String json) {
    ZTreeNode[] deptContacts = new Gson().fromJson(json, ZTreeNode[].class);
    setAdapter(deptContacts);
  }

  public void setAdapter(ZTreeNode[] contents) {
    if (mAdapter == null) {
      mAdapter = new DeptmentListAdapter(getActivity(), contents);
    }
    setListAdapter(mAdapter);
    ((DeptmentListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {

    ZTreeNode content = mAdapter.getItem(position - 1);
    CustomerDialog dialog = new CustomerDialog(content.getName(), content.getCustomAttrs());
    Intent i = new Intent();
    i.putExtra(AppointerListFragment.EXTRA_VALUE, dialog);
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
    super.onListItemClick(l, v, position, id);
  }
}
